package com.controller;


import com.entity.*;
import com.service.*;
import com.util.KeyUtil;
import com.util.StatusCode;
import com.vo.LayuiPageVo;
import com.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


// 学校控制器
@Controller
public class SchoolController {
    @Autowired
    private SchoolService schoolService;
    /**
     * 新增学校
     */
    @ResponseBody
    @PostMapping("/school/insert")
    public ResultVo insertSchool(@RequestBody School school,HttpSession session) {
        // 插入学校
        List<School> school1 = schoolService.LookSchool(school);
        if (school1.size() > 0){
            return new ResultVo(false,StatusCode.ERROR,"该学校已存在");
        }
        Integer i = schoolService.InsertSchool(school);
        SchoolCampus campus = new SchoolCampus();
        campus.setName("主校区");
        campus.setSchoolname(school.getName());
        campus.setSchoolid(school.getId());
        campus.setDescription("主校区");
        if (i == 1){
            schoolService.InsertCampus(campus);
            SchoolCampusDorm dorm = new SchoolCampusDorm();
            dorm.setSchoolid(campus.getSchoolid());
            dorm.setSchoolname(campus.getSchoolname());
            dorm.setCampusid(campus.getId());
            dorm.setCampusname(campus.getName());
            dorm.setName("101");
            schoolService.InsertDorm(dorm);
            return new ResultVo(true, StatusCode.OK,"新增成功");
        }
        return new ResultVo(false,StatusCode.ERROR,"新增失败");
    }

    /**
     * 删除学校
     */
    @ResponseBody
    @PutMapping("/school/delete/{sid}")
    public ResultVo deleteSchool(@PathVariable("sid") Integer sid){
        System.out.println("进来了");
        Integer i = schoolService.DeleteSchool(sid);
        if (i == 1 ){
            return new ResultVo(true, StatusCode.OK,"删除成功");
        }
        return new ResultVo(false, StatusCode.ERROR,"删除失败");
    }

    /**
     * 分页查询学校
     */
    @GetMapping("/school/schoolList/{schoolName}")
    @ResponseBody
    public LayuiPageVo schoolList(int limit, int page, @PathVariable("schoolName") String schoolName) {
        if (schoolName == null || schoolName.equals("") || schoolName.equals("null")){
            schoolName = "";
        }
        List<School> schoolList = schoolService.QuerSchools((page - 1) * limit, limit,schoolName);
        Integer dataNumber = schoolService.QueryAllSchoolCount(schoolName);
        return new LayuiPageVo("",0,dataNumber,schoolList);
    }

    /**
     * 获取所有学校
     */
    @GetMapping("/school/getSchoolList")
    @ResponseBody
    public ResultVo getSchoolList (HttpSession session){
        List<School> schoolList = schoolService.GetSchoolList();
        return new ResultVo(true,StatusCode.OK,"查询成功",schoolList);
    }

    // ====================================  校区 ========================================
    /**
     * 新增校区
     */
    @ResponseBody
    @PostMapping("/campus/insert")
    public ResultVo insertCampus(@RequestBody SchoolCampus campus,HttpSession session) {
        // 插入校区
        List<SchoolCampus> campus1 = schoolService.LookCampus(campus);
        if (campus1.size() > 0){
            return new ResultVo(false,StatusCode.ERROR,"该校区已存在");
        }
        Integer i = schoolService.InsertCampus(campus);
        SchoolCampusDorm one = new SchoolCampusDorm();
        one.setSchoolid(campus.getSchoolid());
        one.setSchoolname(campus.getSchoolname());
        one.setCampusid(campus.getId());
        one.setCampusname(campus.getName());
        one.setName("101");
        if (i == 1){
            schoolService.InsertDorm(one);
            return new ResultVo(true, StatusCode.OK,"新增成功");
        }
        return new ResultVo(false,StatusCode.ERROR,"新增失败");
    }

    /**
     * 删除校区
     */
    @ResponseBody
    @PutMapping("/campus/delete/{sid}")
    public ResultVo deleteCampus(@PathVariable("sid") Integer sid){
        System.out.println("进来了");
        Integer i = schoolService.DeleteCampus(sid);
        if (i == 1 ){
            return new ResultVo(true, StatusCode.OK,"删除成功");
        }
        return new ResultVo(false, StatusCode.ERROR,"删除失败");
    }

    /**
     * 分页查询校区
     */
    @GetMapping("/campus/campusList/{campusName}")
    @ResponseBody
    public LayuiPageVo campusList(int limit, int page, @PathVariable("campusName") String campusName) {
        if (campusName == null || campusName.equals("") || campusName.equals("null")){
            campusName = "";
        }
        List<SchoolCampus> campusList = schoolService.QuerCampus((page - 1) * limit, limit,campusName);
        Integer dataNumber = schoolService.QueryAllCampusCount(campusName);
        return new LayuiPageVo("",0,dataNumber,campusList);
    }

    /**
     * 获取schoolid对应学校的所有校区
     */
    @GetMapping("/school/getCampusListBySchoolId/{schoolId}")
    @ResponseBody
    public ResultVo getCampusListBySchoolId ( @PathVariable("schoolId") Integer schoolId,HttpSession session){
        List<SchoolCampus> schoolCampusList = schoolService.GetSchoolCampusList(schoolId);
        System.out.println(schoolCampusList);
        return new ResultVo(true,StatusCode.OK,"查询成功",schoolCampusList);
    }

    // ====================================  宿舍 ========================================
    /**
     * 新增宿舍
     */
    @ResponseBody
    @PostMapping("/dorm/insert")
    public ResultVo insertDorm(@RequestBody SchoolCampusDorm dorm,HttpSession session) {
        // 插入宿舍
        List<SchoolCampusDorm> dorm1 = schoolService.LookDorm(dorm);
        if (dorm1.size() > 0){
            return new ResultVo(false,StatusCode.ERROR,"该宿舍已存在");
        }
        Integer i = schoolService.InsertDorm(dorm);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"新增成功");
        }
        return new ResultVo(false,StatusCode.ERROR,"新增失败");
    }

    /**
     * 删除宿舍
     */
    @ResponseBody
    @PutMapping("/dorm/delete/{sid}")
    public ResultVo deleteDorm(@PathVariable("sid") Integer sid){
        System.out.println("进来了");
        Integer i = schoolService.DeleteDorm(sid);
        if (i == 1 ){
            return new ResultVo(true, StatusCode.OK,"删除成功");
        }
        return new ResultVo(false, StatusCode.ERROR,"删除失败");
    }

    /**
     * 分页查询宿舍
     */
    @GetMapping("/dorm/dormList/{dormName}")
    @ResponseBody
    public LayuiPageVo dormList(int limit, int page, @PathVariable("dormName") String dormName) {
        if (dormName == null || dormName.equals("") || dormName.equals("null")){
            dormName = "";
        }
        List<SchoolCampusDorm> dormList = schoolService.QuerDorm((page - 1) * limit, limit,dormName);
        Integer dataNumber = schoolService.QueryAllDormCount(dormName);
        return new LayuiPageVo("",0,dataNumber,dormList);
    }


    /**
     * 获取campusid对应校区的所有宿舍
     */
    @GetMapping("/school/getDormListByCampusId/{campusId}")
    @ResponseBody
    public ResultVo getDormListByCampusId ( @PathVariable("campusId") Integer campusId,HttpSession session){
        List<SchoolCampusDorm> schoolCampusDormList = schoolService.GetDormListByCampusId(campusId);
        return new ResultVo(true,StatusCode.OK,"查询成功",schoolCampusDormList);
    }

}

